﻿using System;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace Chimp.Core.Physics
{
    public class ChimpVPhysicsNode : ChimpBase
    {
        public Vector2 Force = Vector2.Zero;
        public float[] LinkDist = new float[5096];
        public List<ChimpVPhysicsNode> Links = new List<ChimpVPhysicsNode>();

        public ChimpVPhysicsNode(float x, float y, float mass)
        {
            Pos = new Vector2(x, y);
            Mass = mass;
        }

        public float Mass { get; set; }
        public bool Static { get; set; }

        public void LinkTo(ChimpVPhysicsNode n)
        {
            if (Links.Contains(n)) return;
            Links.Add(n);
            float xd, yd;
            xd = n.Pos.X - Pos.X;
            yd = n.Pos.Y - Pos.Y;
            var dis = (float) Math.Sqrt(xd*xd + yd*yd);
            LinkDist[Links.Count - 1] = dis;
            n.LinkTo(this);
        }

        public override void OnDraw(Vector2 pos)
        {
            //    base.OnDraw(pos);

            GL.Enable(EnableCap.Blend);

            GL.Color4(0.3f, 0.3f, 0.3f, 0.5f);
            GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.One);
            GL.LineWidth(4);
            GL.Begin(BeginMode.Lines);


            for (int a = 0; a < 360; a += 5)
            {
                LineSeg(a);
                LineSeg(a + 5);
            }

            GL.End();

            GL.LineWidth(1);
        }

        private void LineSeg(int a)
        {
            float lx = ChimpMath.Cos(a)*8.0f*DrawScale;
            float ly = ChimpMath.Sin(a)*8.0f*DrawScale;
            GL.Vertex2(DrawPos.X + lx, DrawPos.Y + ly);
        }
    }
}